package com.hlm.testredis.controller;

import com.hlm.testredis.util.DistributeUtil;
import com.hlm.testredis.mapper.TestDistributeMapper;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @program:test-redis
 * @description:本地数据库室温 分布
 * @author: xinY
 * @create:2020-07-27 09:13
 **/
@RestController
public class TestDistributeController {
    @Autowired
    private TestDistributeMapper mapper;

    @Value("${excelPath}")
    private  String path;
    @GetMapping("/value")
    public String hello(){
        return  path;
    }
    @GetMapping("/")
    public void getData(){
        // 查询
        List<Double> totalList = mapper.findList4allRoomTemp();
        // 处理数据
        System.out.println("totallist"+totalList.size());
        double[] metas = new double[totalList.size()];
        for(int i = 0 ; i < totalList.size() ; i ++){
            metas[i] = totalList.get(i);
        }
        // 分组后数据属性
        Map<String,Double> infoMap = DistributeUtil.infoGroup(metas,0.5 ,6);

        // 分布
        Map<Double,Double> resultMap = DistributeUtil.distribute(metas);

        // 写入excel
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet();
        HSSFRow  row0 = sheet.createRow(0);
        row0.createCell(1).setCellValue("x");
        row0.createCell(2).setCellValue("y");
        row0.createCell(3).setCellValue("avg");
        row0.createCell(4).setCellValue(infoMap.get("average"));
        row0.createCell(5).setCellValue("variance");
        row0.createCell(6).setCellValue(infoMap.get("variance"));
        row0.createCell(7).setCellValue("upLimitGroup:" );
        row0.createCell(8).setCellValue(infoMap.get("upLimitGroup") );
        row0.createCell(9).setCellValue("downLimitGroup:" );
        row0.createCell(10).setCellValue(infoMap.get("downLimitGroup"));
        /*for(int i = 1 ; i < 11 ; i ++){
            System.out.println("===============");
            System.out.println(sheet.toString());
            System.out.println(sheet.getRow(0).toString());
            System.out.println(sheet.getRow(0).getCell(i).toString());
            System.out.println("第"+0+"行第"+i+"列的值是"+sheet.getRow(0).getCell(i).toString());
        }*/

        int i = 0 ;
        for (Double  x : resultMap.keySet()){
            HSSFRow row = sheet.createRow(i+1);
            row.createCell(1).setCellValue(x);
            row.createCell(2).setCellValue(resultMap.get(x));
            i ++;
        }


        FileOutputStream fos ;
        try {
            fos = new FileOutputStream(path);
            workbook.write(fos);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
